#include "lib.h"

extern struct list_head *sct1;
extern int scdata_to_list(SC_data *sc_data ,struct list_head *list );
/*
*
*  编译时记得在选项MISC control 里面添加 --gnu 然后勾选C99. 不然可能编译不过去
*  勾选 USE MicroLIB
*
*
**/


/**
* @brief 展示链表数据，辅助工具
* @param  list 定义链表名称
* @return 
**/

void show_list(void)
{
    SC_data *sc1,*sc2; 
    static int  j=0;
    int i=0;
    UCHAR Sensor_data[20];
    list_for_each_entry_safe(sc1, sc2, sct1, head)
    {
        /* 显示链表内容*/
        if(j == i)
        {   
            sprintf((char *)Sensor_data, "T1:%.1f", (float)sc1->tem_slave_01/10);
            oled_show_string(1, 3, Sensor_data, 4);
            sprintf((char *)Sensor_data, "H1:%.1f", (float)sc1->hum_slave_01/10);
            oled_show_string(1, 5, Sensor_data, 4);
            sprintf((char *)Sensor_data, "T2:%.1f", (float)sc1->tem_slave_02/10);
            oled_show_string(65, 3, Sensor_data, 4);
            sprintf((char *)Sensor_data, "H2:%.1f", (float)sc1->hum_slave_02/10);
            oled_show_string(65, 5, Sensor_data, 4);
            j++;
            if(j >= SC_LIST_MAX)
            {
                j=0;
            }
            // printf("tem_slave_01: %d hum_slave_01: %d \r\n",(float)sc1->tem_slave_01/10,(float)sc1->hum_slave_01/10);
            // printf("tem_slave_02: %d hum_slave_02: %d \r\n",(float)sc1->tem_slave_02/10,(float)sc1->hum_slave_02/10);
            printf("READ mess %d .\r\n",i);
            break;
            
        }
        i++;

        
    }  
    
}

/**
 * @brief 读链表数据到flash中
 * @param  
 * @return  0成功添加进flash  -1  -2 
 */

int read_list_to_flash(struct list_head *list)
{
    SC_data *sc1,*sc2; 
    USHORT save_flash_data[4];
    USHORT count=0;
    list_for_each_entry_safe(sc1, sc2, list, head)
    {
      /* 填入flash的操作*/
      save_flash_data[0] = (sc1->tem_slave_01);  
      save_flash_data[1] = (sc1->hum_slave_01);
      save_flash_data[2] = (sc1->tem_slave_02);  
      save_flash_data[3] = (sc1->hum_slave_02);  
      if(save_flash_data[0]==NULL)
      {
        continue;
      }
      stmflash_write(FLASH_SAVE_ADDR+count,save_flash_data,sizeof(save_flash_data));  
      count+=8;
        

    }
    
    return 0;
}


/**
 * @brief 读flash数据到链表中
 * @param  
 * @return  0成功添加进flash  -1  -2 
 */

int read_flash_to_list(struct list_head *list)
{
    SC_data sc1; 
    USHORT save_flash_data[4];
    USHORT count=0,addr=0;
    for(count=0;count<72;count++)
    {
       stmflash_read(FLASH_SAVE_ADDR+addr ,save_flash_data ,16);
      if(save_flash_data[0]==0xFFFF)
      {
        break;
      }
       addr+=8;
       sc1.tem_slave_01 = save_flash_data[0];
       sc1.hum_slave_01 = save_flash_data[1];
       sc1.tem_slave_02 = save_flash_data[2];
       sc1.hum_slave_02 = save_flash_data[3];
       scdata_to_list(&sc1,list);       
    }
    
    
    return 0;
}



/**
 * @brief 遍历链表查看是否超出限量，并存入数据
 * @param  sc_data 传感器数据及日期信息，可见SC_data结构体 list 定义的列表
 * @return  0成功添加进队列  -1 超出容量 -2 数据错误
 */
int scdata_to_list(SC_data *sc_data ,struct list_head *list )
{
    SC_data *sc1;
    static UCHAR count = 0;
    SC_data *node;

    if(sc_data->hum_slave_01==NULL)
    {   
        printf("sc_data->hum_slave_01 is failed\r\n");
        return -2;
    }

    node = (SC_data *)malloc(sizeof(SC_data));
    node->tem_slave_01 = sc_data->tem_slave_01;
    node->hum_slave_01 = sc_data->hum_slave_01;
    node->tem_slave_02 = sc_data->tem_slave_02;
    node->hum_slave_02 = sc_data->hum_slave_02;
 
    if(count < SC_LIST_MAX)
    {   
        list_add_tail(&node->head, list);
        count++;
    }
    else
    {
        sc1=container_of(sct1->prev,struct switch_cabinet_data,head);
        list_del(&sc1->head);
        free(sc1); 
        sc1=NULL;
        list_add_tail(&node->head, list);
    }
        
    return 0;
}




